<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>面向对象封装消息提示</title>
  <style>
    .modal {
      width: 300px;
      min-height: 100px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
      border-radius: 4px;
      position: fixed;
      z-index: 999;
      left: 50%;
      top: 50%;
      transform: translate3d(-50%, -50%, 0);
      background-color: #fff;
    }

    .modal .header {
      line-height: 40px;
      padding: 0 10px;
      position: relative;
      font-size: 20px;
    }

    .modal .header i {
      font-style: normal;
      color: #999;
      position: absolute;
      right: 15px;
      top: -2px;
      cursor: pointer;
    }

    .modal .body {
      text-align: center;
      padding: 10px;
    }

    .modal .footer {
      display: flex;
      justify-content: flex-end;
      padding: 10px;
    }

    .modal .footer a {
      padding: 3px 8px;
      background: #ccc;
      text-decoration: none;
      color: #fff;
      border-radius: 2px;
      margin-right: 10px;
      font-size: 14px;
    }

    .modal .footer a.submit {
      background-color: #369;
    }
  </style>
</head>

<body>
  <button id="delete">删除</button>
  <button id="login">登录</button>
  <button id="add">新增</button>

  <!-- <div class="modal">
    <div class="header">温馨提示 <i>x</i></div>
    <div class="body">您没有删除权限操作</div>
  </div> -->


  <script>
    // 1.Modal 构造函数的封装
    function Modal(title = '', message = '') {
      this.modalBox = document.createElement('div');
      this.modalBox.innerHTML = `
      <div class="modal">
        <div class="header">${title}<i>x</i></div>
        <div class="body">${message}</div>
      </div>
      `;
      // console.log(this.modalBox.innerHTML);
    }
    // 必须要有实例对象调用构造函数才生效
    // const login = new Modal('友情提示', '您还没有登录');
    // 2. open()方法挂载到原型对象上
    Modal.prototype.open = function () {
      const box = document.querySelector('.modal');
      box && box.remove();
      document.body.append(this.modalBox);
      this.modalBox.querySelector('i').addEventListener('click', () => {
        this.close();
      });
    }
    // 删除按钮
    document.querySelector('#delete').addEventListener('click', () => {
      const del = new Modal('温馨提示', '您没有删除权限操作');
      del.open();
    });
    Modal.prototype.close = function () {
      // Element.remove() 方法，把对象从它所属的 DOM 树中删除。
      this.modalBox.remove();
    };
    // 登录按钮
    document.querySelector('#login').addEventListener('click', () => {
      const login = new Modal('友情提示', '您还没有登录');
      login.open();
    });
    // 新增按钮
    document.querySelector('#add').addEventListener('click', () => {
      const login = new Modal('强烈提示', '您不能新增');
      login.open();
    });

  </script>
</body>

</html>